package com.kitty.game.difu.controller;

import com.kitty.common.cache.RedisService;
import com.kitty.game.bag.message.RespIconCartoon;
import com.kitty.game.confirm.model.DifuZhaoHuanConfirm;
import com.kitty.game.difu.message.ReqApplyGuiChongQinMi;
import com.kitty.game.difu.service.GhostCallChongWu;
import com.kitty.game.difu.service.GhostCallResult;
import com.kitty.game.difu.service.GhostGotoCall;
import com.kitty.game.difu.service.GhostPeiYuan;
import com.kitty.game.difu.service.handler.GhostNingShen;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.pet.PetDataPool;
import com.kitty.game.pet.bean.GhostPetObject;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.pet.service.PetNingShenService;
import com.kitty.game.pet.service.PetPeiYuanService;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.message.RespConfirm;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.concurrent.ThreadLocalRandom;

@Controller
@Slf4j
public class GhostGotoCallController {
    private static final String FIGHT_GOTO_HuanLing = "可前往地府唤灵台处消耗7本#R灵海残卷#n召唤鬼宠，是否前往？";
    @Autowired
    RedisService redisService;
    @Autowired
    EquipService equipService;

    @Autowired
    PetService petService;
    @Autowired
    PetPeiYuanService petPeiYuanService;
    @Autowired
    PetNingShenService petNingShenService;
    /**
     * 点击召唤鬼宠 自动寻路
     * @param session
     * @param ghostGotoCall
     */
    @RequestMapping
    public void openGuiChong(IoSession session, GhostGotoCall ghostGotoCall) {
        Role role = SessionUtils.getRoleBySession(session);
        role.setConfirm(new DifuZhaoHuanConfirm());
        RespConfirm respConfirm = new RespConfirm();
        respConfirm.setTips(FIGHT_GOTO_HuanLing);
        respConfirm.setConfirmText("确定");
        respConfirm.setCancelText("取消");
        MessagePusher.pushMessage(role, respConfirm);
//        GameObjectChar gameObjectChar = session.c();
//        Chara chara = gameObjectChar.chara;
//        SessionUtils.confirm(chara, "可前往地府唤灵台处消耗7本#R灵海残卷#n召唤鬼宠，是否前往？", "gotoHuanLingTai");
    }

    final String[] PET_RANK_GUI_ZU = new String[] { "炼狱魔", "阴阳师", "魅灵", "狱獒" };
    final String[] PET_RANK_GUI_JIANG = new String[] { "范无救", "牛头狱卒", "谢必安", "马面罗刹" };
    final String[] PET_RANK_GUI_XIAN = new String[] { "冥炎之灵", "孟姑", "谛听", "阎摩" };

    /**
     * 鬼宠召唤页面
     * @param session
     * @param ghostCallChongWu
     */
    @RequestMapping
    public void openGuiChong(IoSession session, GhostCallChongWu ghostCallChongWu) {
        log.info("召唤鬼宠");
        Role role = SessionUtils.getRoleBySession(session);
        Integer guizuPrize = Integer.parseInt(DataCache.REDIS_DATA.get("guizu_prize").toString());
        Integer guijiangPrize = Integer.parseInt(DataCache.REDIS_DATA.get("guijiang_prize").toString());
        Integer guixianPrize = Integer.parseInt(DataCache.REDIS_DATA.get("guixian_prize").toString());
        int no1 = guizuPrize;
        int no2 = guizuPrize+guijiangPrize;
        int no3 = no2+guixianPrize;
        int rate = ThreadLocalRandom.current().nextInt(1000);
        String petName = "";
        int penetrate = 6;
        if(rate <= no1){
            int index = ThreadLocalRandom.current().nextInt(PET_RANK_GUI_ZU.length);
            petName = PET_RANK_GUI_ZU[index];
        }else if(rate > no1 && rate<= no2){
             penetrate = 7;
            int index = ThreadLocalRandom.current().nextInt(PET_RANK_GUI_JIANG.length);
            petName = PET_RANK_GUI_JIANG[index];
        }else if(rate > no2){
             penetrate = 8;
            int index = ThreadLocalRandom.current().nextInt(PET_RANK_GUI_XIAN.length);
            petName = PET_RANK_GUI_XIAN[index];
        }
        equipService.delRoloEquip("灵海残卷", 7, true, role);
//        GameObjectChar gameObjectChar = GameObjectChar.getGameObjectChar();
//        Chara chara = gameObjectChar.chara;

        GhostPetObject petObject = PetDataPool.getGhostPetObject(petName);
        petObject.setRank((byte)penetrate);
        Pet pet = petService.addPet(petObject, role, false);
        petService.loadPet(role, pet);

        RespIconCartoon respIconCartoon = new RespIconCartoon();
        respIconCartoon.setType((short) 2);
        respIconCartoon.setName(petName);
        respIconCartoon.setParam(petObject.getIcon() + "");
        MessagePusher.pushMessage(role, respIconCartoon);

        RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
        respGeneralNotify.setNotify(50001);
        respGeneralNotify.setValue(petObject.getName());
        MessagePusher.pushMessage(role, respGeneralNotify);

        GhostCallResult ghostCallResult = new GhostCallResult();
        ghostCallResult.setA(1);
        MessagePusher.pushMessage(role, ghostCallResult);
    }
    @RequestMapping
    public void guiChongPeiYuan(IoSession session, GhostPeiYuan ghostPeiYuan) {
        petPeiYuanService.startPeiYuan(session,ghostPeiYuan);
    }

    @RequestMapping
    public void guiChongNingShen(IoSession session, GhostNingShen ghostNingShen) {
        petNingShenService.startNingShen(session,ghostNingShen);
    }

    @RequestMapping
    public void applyGuiChongQinMi(IoSession session, ReqApplyGuiChongQinMi reqApplyGuiChongQinMi) {
        petNingShenService.applyGuiChongQinMi(session,reqApplyGuiChongQinMi);
    }
}
